import _ from 'lodash'
import * as productHelper from "helpers/productHelper";
import {getProduct} from "api/productApi";
import {getCartsCount} from "api/cartApi";

export default {
    state: () => ({
        product: null,
        /**
         * All the skus of certain product.
         */
        skus: [],
        /**
         * Once there is no any selected sku,
         * then showing this default sku,
         * otherwise showing the selected sku.
         */
        defaultSku: null,
        selectedSku: null,
        selectedSkuCount: 1,
		shoppingCartNum : 0,

        /**
         * All available attribute keys and values of certain product.
         */
        attributes: [],
        attributeKeysCount: 0,
        originalAttrValueIds: [],
        selectedAttributeValues: [],

        largeSkuCover: null,
				group_id:null
    }),
    getters: {
        product: state => state.product,
        skus: state => state.skus,
        defaultSku: state => state.defaultSku,
        selectedSku: state => state.selectedSku,
        selectedSkuCount: state => state.selectedSkuCount,
		shoppingCartNum: state => state.shoppingCartNum,

        attributes: state => state.attributes,
        hasAttribute: state => {
            return state.attributeKeysCount > 0
        },
        attributeKeysCount: state => state.attributeKeysCount,
        originalAttrValueIds: state => state.originalAttrValueIds,
        selectedAttributeValues: state => state.selectedAttributeValues,

        largeSkuCover: state => state.largeSkuCover,
				group_id:state => state.group_id,
    },
    mutations: {
        setProduct(state, product = null) {
            state.product = product
        },
        setSkus(state, skus = []) {
            state.skus = skus
        },
        setDefaultSku(state, sku = null) {
            state.defaultSku = sku
        },
        setSelectedSku(state, sku = null) {
            state.selectedSku = sku
        },
        setSelectedSkuCount(state, count = 1) {
            state.selectedSkuCount = count
        },
		setShoppingCartNum(state, count = 1) {
		    state.shoppingCartNum = count
		},
        setAttributes(state, attributes = []) {
            state.attributes = attributes
        },
        setAttributeKeysCount(state, count = 0) {
            state.attributeKeysCount = count
        },
        setOriginalAttrValueIds(state, ids = []) {
            state.originalAttrValueIds = ids
        },
        setSelectedAttributeValues(state, values = []) {
            state.selectedAttributeValues = values
        },
        setLargeSkuCover(state, cover = null) {
            state.largeSkuCover = cover
        },
				setGroupId(state, group_id = null){
					state.group_id = group_id
				}
    },
    actions: {
        async getCurrentProduct({ dispatch, commit }, id) {
            commit('setProduct', null)
            dispatch('updateLargeSkuCover', null)
            const product = await getProduct(id)
            commit('setProduct', product)
            dispatch('initAllSkus', product)
			console.log(111);
            dispatch('initDefaultSku', product)
            dispatch('initAllAttributes', product)
            dispatch('initAttributeKeysCount', product)
        },
        async initAllSkus({ commit }, product) {
            commit('setSkus', product.skus)
        },
        async initDefaultSku({ commit }, product) {
            const defaultSku = productHelper.initDefaultSku(product)
			console.log(123);
            commit('setDefaultSku', defaultSku)
            commit('setSelectedSku', null)
						
						// if(product.is_gold_bean){
						// 	commit('setSelectedSku', defaultSku)
						// }else{
						// 	commit('setSelectedSku', null)
						// }
        },
        async initAllAttributes({ state, commit }, product) {
            if (_.isEmpty(product.attribute_group)) {
                commit('setAttributes', [])
                commit('setSelectedSkuCount', 1)
                return
            }
            const originalAttrValueIds = productHelper.initOriginalAttrValueIds(product)
            commit('setOriginalAttrValueIds', originalAttrValueIds)
            commit('setSelectedAttributeValues', [])
						
            commit('setSelectedSkuCount', 1)
            commit('setAttributes', product.attribute_group.attribute_keys)
            const res = productHelper.refreshAttribute()
            commit('setAttributes', res.attributes)
        },
        async initAttributeKeysCount({ commit }, product) {
            if (_.isEmpty(product.attribute_group)) {
                commit('setAttributeKeysCount', 0)
                return
            }
            const keysCount = productHelper.initAttributeKeysCount(product)
            commit('setAttributeKeysCount', keysCount)
        },
        async refreshSelectedSkuCount({ commit }, count) {
            commit('setSelectedSkuCount', count)
        },
		async refreshShoppingCartNum({ commit }, count) {
		    commit('setShoppingCartNum', count)
		},
		async getUserCartsCount({ commit }) {
		    const response = await getCartsCount()
		    commit('setShoppingCartNum', response.total)
		},
        async refreshAttributes({ state, commit }, params = {}) {
            const keyId = params.keyId
            const valueId = params.valueId
            const isCancel = params.isCancel
            const res = productHelper.refreshAttribute(keyId, valueId, isCancel)
						// console.log(res)
            commit('setSelectedAttributeValues', res.selectedValueIds)
            commit('setAttributes', [])
            commit('setAttributes', res.attributes)
            const sku = productHelper.matchSelectedFinalSku()
            if (sku) {commit('setSelectedSku', sku)} else{
							commit('setSelectedSku', null)
						}
        },
        async directSelectSku({ state, commit }, skuId) {
            const sku = _.find(state.skus, ['id', skuId])
            if (sku) {commit('setSelectedSku', sku)}
        },
        async updateLargeSkuCover({ state, commit }, cover) {
            commit('setLargeSkuCover', cover)
        }
    }
}
